/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tos.bakery;

import org.jdesktop.application.FrameView;
import tos.TosView;

/**
 *
 * @author Milos
 */
public class Bakery {

    public Bakery(FrameView parent) {
        Brojac brojac = new Brojac(0);
        int noOfProcess = Integer.parseInt(((TosView) parent).brojProcesa.getText());
        int increment = Integer.parseInt(((TosView) parent).povecanje.getText());
        Variables vars = new Variables(noOfProcess);
        Processes[] processes = new Processes[noOfProcess];
        for (int i = 0; i < noOfProcess; i++) {
            processes[i] = new Processes(vars, i, brojac, increment, parent);
        }

        ((TosView) parent).appendOutput("Broj je " + brojac.getBroj() + "\n");
        for (int i = 0; i < noOfProcess; i++) {
            processes[i].start();
        }

        try {
            for (int i = 0; i < noOfProcess; i++) {
                processes[i].join();
            }
        } catch (InterruptedException ex) {
            System.out.println("Erorr!!" + ex.toString());
        }


        ((TosView) parent).appendOutput("Broj je " + brojac.getBroj() + "\n");
    }
}
